<?php
class CouponMode extends SiteMode {
    /** @return CouponMode */
    public static function init() {
        return parent::init();
    }

    /**
     * 获取用户优惠券
     * @param $user_id
     * @param null $shop_id 优惠券商家
     * @param bool $useful  是否只搜索有效优惠券
     * @return array
     */
    function getListByUser($user_id,$shop_id = null,$useful = false){
        $condition='';
        if(!empty($shop_id)){
            $condition.=" AND SI.`id`= ".(int)$shop_id;
        }
        if($useful){
            $condition.=" AND UC.`used`=0 AND C.`deadline`>=".$this->db->quote(date('Y-m-d',time()));
        }
        $sql="SELECT UC.*,C.`value` as `coupon_value`,C.`deadline`,C.`remark`,SI.`company`
              FROM `user_coupon` UC LEFT JOIN `coupon` C ON UC.`coupon_id`=C.`id`
              LEFT JOIN `shop_info` SI ON C.`shop_id`=SI.`id`
              WHERE UC.`user_id`=? and C.site_id=? $condition";
        return $this->db->getAll($sql, (int)$user_id, $this->siteId);
    }
    //获取某商家所有优惠券,这个就不用分页了吧。要分页再说
    function getListByShop($shop_id){
        $sql="SELECT * FROM `coupon` WHERE `shop_id`=? and site_id=?";
        return $this->db->getAll($sql,$shop_id, $this->siteId);
    }
    //添加 shop_id为0表示通用
    function add($shop_id,$value,$deadline = null,$remark = null){
        $data['shop_id']=$shop_id;
        $data['value']=$value;
        $data['deadline']=$deadline;
        $data['remark']=$remark;
        $data['site_id']=$this->siteId;
        return $this->db->insert('coupon',$data) == 1 ? (int)$this->db->insertId():-1;
    }

    //用户使用优惠券
    function using($user_coupon_id,$order_id){
        $data['used']=1;
        $data['used_time']=date('Y-m-d H:i:s',time());
        $data['order_id']=$order_id;
        return $this->db->update('user_coupon',$user_coupon_id,$data)==1;
    }
    //根据id获取单个优惠券信息,商家信息暂时只搜了公司名
    function getDetail($id){
        $sql="SELECT C.*,SI.`company` FROM `coupon` C,`shop_info` SI
              WHERE C.`shop_id`=Si.`id` AND C.`id`=?";
        return $this->db->getOne($sql,$id);
    }
    function getDetailByUser($user_coupon_id){
        $user_coupon_id=(int)$user_coupon_id;
        $sql="SELECT `coupon_id`,`value` FROM `user_coupon` WHERE `id`=?";
        $info=$this->db->getOne($sql,$user_coupon_id);
        $couponData=$this->getDetail($info['coupon_id']);
        if(!empty($info['value'])){
            $couponData['value']=$info['value'];
        }
        return $couponData;
    }
    // 所有优惠券信息
    function getAll($shop_id = null ,$rows = 20, $offset = 0){
        $rows=(int)$rows;
        $offset=(int)$offset;
        $condition='';
        if(!empty($shop_id)){
            $condition.=" AND SI.`id`=".(int)$shop_id;
        }
        $sql="SELECT C.*,SI.`company` FROM `coupon` C,`shop_info` SI
              WHERE C.`shop_id`=SI.`id` $condition limit $offset,$rows";
        return $this->db->getList($sql);
    }
    //根据user_coupon_id获得优惠券value
    function getValueByUser($user_coupon_id){
        $user_coupon_id=(int)$user_coupon_id;
        $sql="SELECT `coupon_id`,`value` FROM `user_coupon` WHERE `id`=?";
        $info=$this->db->getOne($sql,$user_coupon_id);
        if(!empty($info['coupon_id'])){
            $couponInfo = $this->getDetail($info['coupon_id']);
            return $couponInfo['value'];
        }else{
            return $info['value'];
        }
    }

} 